package com.atguigu.redis_springboot.base;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;

import java.util.HashSet;
import java.util.Set;

/**
 * 哨兵测试
 */
public class JedisSentinelTest {

    public static void main(String[] args) {
        Set<String> sentinelSet = new HashSet<String>();
        sentinelSet.add("192.168.200.11:26379");

        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(10); //最大可用连接数
        jedisPoolConfig.setMaxIdle(5); //最大闲置连接数
        jedisPoolConfig.setMinIdle(5); //最小闲置连接数
        jedisPoolConfig.setBlockWhenExhausted(true); //连接耗尽是否等待
        jedisPoolConfig.setMaxWaitMillis(2000); //等待时间
        jedisPoolConfig.setTestOnBorrow(true); //取连接的时候进行一下测试 ping pong
        /**
         * 哨兵服务器配置
         * sentinel monitor masteralias 127.0.0.1 6380 1
         * sentinel auth-pass masteralias 123
         */
        JedisSentinelPool jedisSentinelPool = new JedisSentinelPool("masteralias", sentinelSet, jedisPoolConfig);
        jedisSentinelPool.getResource();
//        Jedis jedis = jedisSentinelPool.getResource();
//        System.out.println(jedis.ping());
    }

    /**
     * 192.168.200.11
     * Exception in thread "main" redis.clients.jedis.exceptions.JedisExhaustedPoolException: Could not get a resource since the pool is exhausted
     * 	at redis.clients.jedis.util.Pool.getResource(Pool.java:53)
     * 	at redis.clients.jedis.JedisSentinelPool.getResource(JedisSentinelPool.java:213)
     * 	at com.atguigu.redis_springboot.base.JedisSentinelTest.main(JedisSentinelTest.java:28)
     * Caused by: java.util.NoSuchElementException: Unable to validate object
     * 	at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:498)
     * 	at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:365)
     * 	at redis.clients.jedis.util.Pool.getResource(Pool.java:50)
     * 	... 2 more
     */


    /**
     * 127.0.0.1
     *Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
     * 	at redis.clients.jedis.util.Pool.getResource(Pool.java:59)
     * 	at redis.clients.jedis.JedisSentinelPool.getResource(JedisSentinelPool.java:213)
     * 	at com.atguigu.redis_springboot.base.JedisSentinelTest.main(JedisSentinelTest.java:28)
     * Caused by: redis.clients.jedis.exceptions.JedisConnectionException: Failed connecting to host 127.0.0.1:6380
     * 	at redis.clients.jedis.Connection.connect(Connection.java:204)
     * 	at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:100)
     * 	at redis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1866)
     * 	at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:117)
     * 	at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:883)
     * 	at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:436)
     * 	at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:365)
     * 	at redis.clients.jedis.util.Pool.getResource(Pool.java:50)
     * 	... 2 more
     * Caused by: java.net.SocketTimeoutException: connect timed out
     * 	at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
     * 	at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
     * 	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
     * 	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
     * 	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
     * 	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
     * 	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
     * 	at java.net.Socket.connect(Socket.java:589)
     * 	at redis.clients.jedis.Connection.connect(Connection.java:181)
     * 	... 9 more
     *
     *
     *
     */
}
